package com.feizhoutuobang.shapeline.util.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtils {

	public static void printText(File from, File to, Color color, 
			String[] texts, String[] families, int[] styles, int[] sizes, int[] tops, 
			int fullWidth) throws IOException {
		
		Image img = ImageIO.read(from);
		int width1 = img.getWidth(null);
		int height1 = img.getHeight(null);
		
		BufferedImage image = new BufferedImage(width1, height1, 1);
		Graphics g = image.createGraphics();
		g.drawImage(img, 0, 0, width1, height1, null);
		g.setColor(color);
		
		for (int i = 0; i < texts.length; i ++) {
			g.setFont(new Font(families[i], styles[i], sizes[i]));
			int width = g.getFontMetrics().stringWidth(texts[i]);
			int left = (fullWidth - width) / 2;
			g.drawString(texts[i], left, tops[i]);
		}
		g.dispose();
		
		FileOutputStream os = null;
		try {
			os = new FileOutputStream(to);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
			encoder.encode(image);
		}
		finally {
			try {os.close(); } catch (Exception e) {}
		}
	}
}
